<title>应用管理</title>
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>应用管理</cite></a>
  </div>
</div>


<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab-content">
      <div class="layui-btn-group demoTable lay-mb15">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist">
          <div class="layui-form-item" style="margin:0;">
            <div class="layui-form-item" style="margin:0;">
              <div class="layui-inline">
                <label class="layui-form-label">应用ID</label>
                <div class="layui-input-block">
                  <input type="text" name="appId" id="appId" placeholder="应用ID" autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-inline">
                <label class="layui-form-label">商户ID</label>
                <div class="layui-input-block">
                  <input type="text" name="mchId" id="mchId" placeholder="商户ID" autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-inline">
                <button id="search" class="layui-btn layuiadmin-btn-admin" data-type="reload">
                  <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
              </div>

            </div>
          </div>
        </div>
      </div>
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">

          <!--<button id="addChannelMch" class="layui-btn layui-btn-normal" data-type="add"><i class="layui-icon"></i>添加-->
          <!--</button>-->
          <!--<button id= "output" class="layui-btn layui-btn-warm" data-type="output"><i class="layui-icon layui-icon-download-circle"></i>导出-->
          <!--</button>-->
          <!--<button id= "batchdel" class="layui-btn layui-btn-danger" data-type="batchdel"><i class="layui-icon"></i>删除-->
          <!--</button>-->
        </div>
        <table id="XxPay_Mgr_App_dataAll" lay-filter="XxPay_Mgr_App_dataAll"></table>
      </div>
    </div>
  </div>
</div>

<script type="text/html" id="appbar">
  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">查看</a>
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<script>
  layui.use(['admin', 'table', 'util'], function(){
    var $ = layui.$
      ,admin = layui.admin
      ,table = layui.table
      ,element = layui.element
      ,form = layui.form;

    element.render('breadcrumb', 'breadcrumb');

    var tplStatus = function(d){
        if(d.status == 1) {
            return '  <input type="checkbox" name="status" lay-filter="layTableSwitchForApp" lay-skin="switch" lay-text="开|关" checked="true"> <div class="layui-unselect layui-form-switch" lay-skin="_switch"><em>开</em> <i></i></div>';

        }
        return ' <input type="checkbox" name="status" lay-filter="layTableSwitchForApp" lay-skin="switch" lay-text="开|关" > <div class="layui-unselect layui-form-switch" lay-skin="_switch"><em>关</em><i></i></div>';

    };


      form.on("switch(layTableSwitchForApp)",function(data){
          //  layer.msg(this.checked ? "true" : "false");

          var index_sms;
          // 获取当前控件
          var selectIfKey=data.othis;
          // 获取当前所在行
          var parentTr = selectIfKey.parents("tr");
          // 获取当前所在行的索引
          var parentTrIndex = parentTr.attr("data-index");
          var appid=parentTr.find(('td:eq(3)')).text().trim();
          admin.req({
              type: 'get',
              url: layui.setter.baseUrl + '/app/updateStatus',
              data: {
                  appId : appid,
                  status : this.checked ? "true" : "false"
              },
              beforeSend:function(){
                  index_sms = layer.msg('正在切换中，请稍候',{icon: 16,time:false,shade:0.8});
              },
              error: function(err){
                  layer.alert(JSON.stringify(err.field), {
                      title: '错误提示'
                  })
              },
              success: function(res){
                  if(res.code == 0){
                      layer.close(index_sms);
                      layer.msg('修改应用状态成功', {icon: 6});
                  }
              }
          });
      })

    //商户列表
    table.render({
      elem: '#XxPay_Mgr_App_dataAll'
      ,url: layui.setter.baseUrl + '/app/list' //列表接口
      ,where: {
          access_token: layui.data(layui.setter.tableName).access_token
      }
      ,id: 'tableReload'
      ,page: true
      ,cols: [[
        {type: 'checkbox', fixed: 'left'}
            ,{field: 'mchId', title: '商户ID', fixed: 'left',width:100}
        ,{field: 'appName', title: '应用名称',width:120}
            ,{field: 'appId', title: '应用ID',width:300}

        ,{field: 'status', title: '状态', templet: tplStatus}
        ,{field: 'createTime', title: '创建时间', width: 200, templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'}
        ,{field: 'edit', title: '操作', toolbar: '#appbar' }
      ]]
      ,skin: 'line'
    });

    //监听工具条
    table.on('tool(XxPay_Mgr_App_dataAll)', function(obj){
      var data = obj.data;
      if(obj.event === 'detail'){
        location.hash = "/merchant/app/app_view/appId=" + data.appId;
      } else if(obj.event === 'edit'){
        location.hash = "/merchant/app/app_edit/appId=" + data.appId;
      }

    });

    // 搜索
    var $ = layui.$, active = {
      reload: function(){
        var mchId = $('#mchId').val();
        var appId = $("#appId").val();
        //执行重载
        table.reload('tableReload', {
          page: {
            curr: 1 //重新从第 1 页开始
          }
          ,where: {
            mchId: mchId,
            appId : appId
          }
        });
      }
    };
    $('#search').on('click', function() {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    // 渲染表单
    form.render();
  });
</script>